package com.virtual.repository.common.model;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

import com.virtual.repository.common.model.adapters.MappingMapAdapter;

/**
 * Class representing the <strong>model-config</strong> element from configuration file.
 * It is the root element of the whole configuration.  
 * 
 * @author kchaber
 * 
 */
@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement(name = "model-config")
public class ModelConfiguration implements Serializable {
	private static final long serialVersionUID = 3724478570642557369L;

	/**
	 * The list of all available databases.
	 */
	@XmlElementWrapper(name = "data-sources")
	@XmlElement(name = "data-source")
	private List<DataSource> dataSources;
	
	/**
	 * The list of all mappings between Java and databases.
	 * Indexed by class name
	 */
	@XmlElement(name = "mappings")
	@XmlJavaTypeAdapter(MappingMapAdapter.class)
	private Map<String, Mapping> mappings;
	
	public List<DataSource> getDataSources() {
		return dataSources;
	}

	public void setDataSources(List<DataSource> dataSources) {
		this.dataSources = dataSources;
	}

	public Map<String, Mapping> getMappings() {
		return mappings;
	}
	
	public Mapping getMapping(String className) {
		return this.mappings.get(className);
	}

	public void setMappings(Map<String, Mapping> mappings) {
		this.mappings = mappings;
	}

	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append(getClass().getName());
		
		builder.append(" [\n\tdataSources=");
		for (DataSource dt : dataSources) {
			builder.append("\n\t\t").append(dt);
		}
		
		builder.append(" [\n\tmappings=");
		for (Entry<String, Mapping> m : mappings.entrySet()) {
			builder.append("\n\t\t").append(m.getValue());
		}
		
		return builder.toString();
	}

}
